<template>
  <div class="q-pa-sm report-style">
    <div class="q-gutter-y-md">
      <q-table
        class="card-radius my-table"
        wrap-cells
        flat
        bordered
        :title="'Version Master'"
        title-class="page-header"
        :columns="columns"
        :rows="filterRows"
        :pagination="{ rowsPerPage: 15 }"
        row-key="id"
        no-data-label="No version found."
        no-results-label="The filter didn't uncover any results"
        @row-dblclick="dblclickFn"
      >
        <template v-slot:top>
          <div class="row justify-between" style="width: 100%">
            <div class="fal page-header">Version Master</div>
            <div>
              <q-card-actions>
                <div class="row justify-end q-px-md">
                  <q-btn
                    class="default-btn"
                    label="Create"
                    @click="createDiag = true"
                  />
                </div>
              </q-card-actions>
            </div>
          </div>
        </template>
        <template v-slot:top-row="props">
          <ColsFilter :filter="props.cols" ref="colsFilter"></ColsFilter>
      </template>

        <template v-slot:body-cell-actions="props">
          <q-td>
            <div class="text-left">
              <q-btn
                dense
                round
                flat
                class="edit-btn"
                icon="edit"
                @click.stop="openEditDiag(props.row.id)"
              ></q-btn>
              <q-btn
                dense
                round
                flat
                class="del-btn"
                @click="deleteFn(props.row.id)"
                icon="delete"
              ></q-btn>
            </div>
          </q-td>
        </template>

        <template v-slot:no-data="{ icon, message }">
          <div class="full-width row flex-center text-accent q-gutter-sm">
            <!-- <q-icon size="2em" name="sentiment_dissatisfied" /> -->
            <q-icon size="2em" :name="icon" />
            <span> {{ message }} </span>
          </div>
        </template>
      </q-table>
    </div>

    <q-dialog  v-model="createDiag">
      <version-form @submit="initData" @close="createDiag = false" />
    </q-dialog>
    <q-dialog  v-model="editDiag">
      <version-form
        mode="edit"
        @submit="initData"
        @close="editDiag = false"
        :id="editId"
      />
    </q-dialog>
  </div>
</template>

<script setup lang="ts">
import { QTableColumn, useQuasar } from 'quasar';
import { computed, onMounted, ref } from 'vue';
import VersionForm from 'src/components/version/VersionForm.vue';
import { Version } from 'src/services/type';
import { deleteVersionMaster, listVersions } from 'src/services/versionService';
import { useVersionStore } from 'src/stores/versionStore';
import ColsFilter from 'src/components/table/ColsFilter.vue';

const $q = useQuasar();

const versionStore = useVersionStore();

const colsFilter = ref<typeof ColsFilter>();

const align = 'left';

const columns = [
/*   {
    name: 'id',
    label: '#',
    field: 'id',
    sortable: true,
    search: false,
    align,
  }, */
  {
    name: 'stock_code',
    label: 'Stock Code',
    field: 'stock_code',
    sortable: true,
    search: true,
    align,
  },
  {
    name: 'description',
    label: 'Short Description',
    field: 'description',
    sortable: true,
    search: true,
    align,
  },

  {
    name: 'equipment',
    label: 'Equipment Class',
    field: 'equipment',
    sortable: true,
    search: true,
    align,
  },

  {
    name: 'sw_ver',
    label: 'S/W Version',
    field: 'sw_ver',
    sortable: true,
    search: true,
    align,
  },
  {
    name: 'hw_ver',
    label: 'H/W Version',
    field: 'hw_ver',
    sortable: true,
    search: true,
    align,
  },
  {
    name: 'actions',
    label: 'Actions',
    search: false,
    field: 'actions',
    align,
  },
];

const data = ref<Version[]>([]);
/* const filter = ref(''); */
const createDiag = ref(false);
const editDiag = ref(false);
const editId = ref(0);

async function initData() {
  const versions = await listVersions();
  data.value = versions;
  versionStore.init();
}

function openEditDiag(id: number) {
  editId.value = id;
  editDiag.value = true;
}

function dblclickFn(evt: any, row: Version, index: number) {
  editId.value = row.id;
  editDiag.value = true;
}

function filterFn(row: Version) {
  if (colsFilter.value) {
    return colsFilter.value.filterFn(row);
  }
}

const filterRows = computed(() => {
  return data.value?.filter(filterFn);
});

function deleteFn(id: number) {
  $q.dialog({
    title: 'Confirm',
    message: 'Are you sure to delete this record?',
    cancel: true,
  }).onOk(async () => {
    const res = await deleteVersionMaster(id);

      $q.notify({
        message: 'Success',
        color:'positive'
      })
      initData();

  });
}

onMounted(() => {
  initData();
});
</script>

<style scoped>
.content {
  display: flex;
  justify-content: space-between;
  background-color: #eef4fa;
  padding-top: 20px;
  padding-right: 10px;
}

.warning {
  color: red;
  font-weight: bold;
  text-align: center;
  width: -webkit-fill-available;
  padding-top: 20px;
}

th,
td {
  text-align: center;
  padding: 10px;
}

.row-bg {
  background-color: #d9d9d9;
}

.row-bg-light {
  background-color: #eeeded;
}
</style>
